<template>
  <div class="weather-container">
    <el-skeleton :loading="loading" animated>
      <template #default>
        <div class="weather-content">
          <!-- 当前天气 -->
          <div class="current-weather">
            <div class="location">
              <Icon icon="ep:location" class="location-icon" />
              <span>{{ weatherData.place }}</span>
            </div>
            <div class="today-weather" v-if="todayWeather">
              <div class="today-left">
                <img :src="todayWeather.img1" class="weather-icon" alt="天气图标" />
                <div class="today-temp">{{ todayWeather.wendu1 }}</div>
              </div>
              <div class="today-right">
                <div class="today-info">{{ todayWeather.wea1 }}</div>
                <div class="today-date">
                  <span>今天</span>
                  <span class="date-text">{{ todayWeather.week2 }}</span>
                  <span class="week-text">{{ todayWeather.week1 }}</span>
                </div>
                <div class="temp-range">{{ todayWeather.wendu2 }} ~ {{ todayWeather.wendu1 }}</div>
              </div>
            </div>
          </div>

          <!-- 天气预报 -->
          <div class="forecast-container">
            <el-scrollbar>
              <div class="forecast-list">
                <div
                  v-for="(item, index) in weatherData.data"
                  :key="index"
                  class="forecast-item"
                  :class="{ 'active': index === 0 }"
                >
                  <div class="forecast-day">{{ index === 0 ? '今天' : item.week1 }}</div>
                  <div class="forecast-date">{{ item.week2 }}</div>
                  <img :src="item.img1" class="forecast-icon" alt="天气图标" />
                  <div class="forecast-weather">{{ item.wea1 }}</div>
                  <div class="forecast-temp">{{ item.wendu2 }} ~ {{ item.wendu1 }}</div>
                </div>
              </div>
            </el-scrollbar>
          </div>
        </div>
      </template>
    </el-skeleton>
  </div>
</template>

<script setup lang="ts">
import {get15DaysWeather, WeatherDayInfo, WeatherResponse} from '@/api/infra/weather'

defineOptions({ name: 'WeatherComponent' })

const loading = ref(true)
const weatherData = ref<WeatherResponse>({
  data: [],
  code: 0,
  place: ''
})

const todayWeather = computed<WeatherDayInfo | null>(() => {
  return weatherData.value.data && weatherData.value.data.length > 0
    ? weatherData.value.data[0]
    : null
})

// 获取天气数据
const getWeatherData = async () => {
  try {
    loading.value = true
    const res = await get15DaysWeather()
    weatherData.value = res
  } catch (error) {
    console.error('获取天气数据失败', error)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  getWeatherData()
})
</script>

<style lang="scss" scoped>
.weather-container {
  width: 100%;
  border-radius: 8px;
  overflow: hidden;
}

.weather-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.current-weather {
  padding: 8px 0;
}

.location {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #606266;
  margin-bottom: 8px;

  .location-icon {
    margin-right: 4px;
    color: #409EFF;
  }
}

.today-weather {
  display: flex;
  align-items: center;

  .today-left {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-right: 16px;

    .weather-icon {
      width: 60px;
      height: 60px;
    }

    .today-temp {
      font-size: 28px;
      font-weight: bold;
      color: #303133;
    }
  }

  .today-right {
    display: flex;
    flex-direction: column;

    .today-info {
      font-size: 18px;
      font-weight: 500;
      color: #303133;
    }

    .today-date {
      margin: 4px 0;
      font-size: 14px;
      color: #606266;

      .date-text {
        margin: 0 8px;
      }

      .week-text {
        color: #909399;
      }
    }

    .temp-range {
      font-size: 14px;
      color: #606266;
    }
  }
}

.forecast-container {
  width: 100%;
}

.forecast-list {
  display: flex;
  padding: 8px 0;
}

.forecast-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 80px;
  padding: 8px;
  border-radius: 8px;
  transition: all 0.3s;

  &.active {
    background-color: rgba(64, 158, 255, 0.1);
  }

  &:hover {
    background-color: rgba(64, 158, 255, 0.1);
  }

  .forecast-day {
    font-size: 14px;
    font-weight: 500;
    color: #303133;
  }

  .forecast-date {
    font-size: 12px;
    color: #909399;
    margin-bottom: 4px;
  }

  .forecast-icon {
    width: 36px;
    height: 36px;
    margin: 4px 0;
  }

  .forecast-weather {
    font-size: 12px;
    color: #606266;
  }

  .forecast-temp {
    font-size: 12px;
    color: #606266;
    margin-top: 4px;
  }
}
</style>

